package com.yb.sys.user.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;

import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/11/23 16:22
 * FileName: PageResult
 * Description:
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PageResult{
    private int page;
    private int size;

    @JsonIgnore
    private Sort sort;
    private long total;
    private int totalPage;

    private Map<String,Sort.Direction> sorts;
    private Object records;



    public <T> PageResult  resultPage(Page<T> resultPage) {
        if(Objects.isNull(resultPage))return this;
        this.page=resultPage.getNumber();
        this.size=resultPage.getSize();
        this.sort=resultPage.getSort();
        this.total=resultPage.getTotalElements();
        this.records=resultPage.get().collect(Collectors.toList());
        this.totalPage=resultPage.getTotalPages();
        return this;
    }


    public PageResult pageReq(PageReq pageReq) {
        this.sorts=pageReq.getSorts();
        return this;
    }
}
